# Tweeting #RamNavami
# Using R version 4.0.5 (2020-03-31) -- "Shake and Throw"
# April 26, 2021

# Set Working Directory

setwd("C://Replication_Data")

# Open Libraries
# Note: statnet version 2019.6, created on 2019-06-13
# Note: informR Version 1.0-5 created on 2015-03-09.

library(statnet)
library(informR)

# Read Data

Attributes_Two_or_More_Hashtags <- read.csv("Attributes_Two_or_More_Hashtags.csv", header=TRUE)
Attributes_Top_30_Hashtags <- read.csv("Attributes_Top_30_Hashtags.csv", header=TRUE)
Edges_Two_or_More_Hashtags <- read.csv("Edges_Two_or_More_Hashtags.csv", header=TRUE)
Edges_Top_30_Hashtags <- read.csv("Edges_Top_30_Hashtags.csv", header=TRUE)
eventlist <- read.csv("Eventlist_RamNavami.csv", header=TRUE)

# Convert Data to Network Form

Hashtag_Edges_Two_or_More_Matrix <- as.matrix(Edges_Two_or_More_Hashtags, directed=FALSE)
Hashtag_Edges_Two_or_More_Network <- as.network(Hashtag_Edges_Two_or_More_Matrix, bipartite=1668, directed=FALSE)
Hashtag_Edges_Top_30_Matrix <- as.matrix(Edges_Top_30_Hashtags, directed=FALSE)
Hashtag_Edges_Top_30_Network <- as.network(Hashtag_Edges_Top_30_Matrix, bipartite=1389, directed=FALSE)

# Attach Attributes to Network 

Hashtag_Edges_Two_or_More_Network %v% "B1_Followed" <- Attributes_Two_or_More_Hashtags$B1_Followed
Hashtag_Edges_Two_or_More_Network %v% "B1_Followers" <- Attributes_Two_or_More_Hashtags$B1_Followers
Hashtag_Edges_Two_or_More_Network %v% "B1_Tweets" <- Attributes_Two_or_More_Hashtags$B1_Tweets
Hashtag_Edges_Two_or_More_Network %v% "B2_DirectlyReferncesRamNavami" <- Attributes_Two_or_More_Hashtags$B2_DirectlyReferncesRamNavami
Hashtag_Edges_Two_or_More_Network %v% "B2_ReligiousConnotation" <- Attributes_Two_or_More_Hashtags$B2_ReligiousConnotation
Hashtag_Edges_Two_or_More_Network %v% "B2_Covid" <- Attributes_Two_or_More_Hashtags$B2_Covid

Hashtag_Edges_Top_30_Network %v% "B1_Followed" <- Attributes_Top_30_Hashtags$B1_Followed
Hashtag_Edges_Top_30_Network %v% "B1_Followers" <- Attributes_Top_30_Hashtags$B1_Followers
Hashtag_Edges_Top_30_Network %v% "B1_Tweets" <- Attributes_Top_30_Hashtags$B1_Tweets
Hashtag_Edges_Top_30_Network %v% "B2_DirectlyReferncesRamNavami" <- Attributes_Top_30_Hashtags$B2_DirectlyReferncesRamNavami
Hashtag_Edges_Top_30_Network %v% "B2_ReligiousConnotation" <- Attributes_Top_30_Hashtags$B2_ReligiousConnotation
Hashtag_Edges_Top_30_Network %v% "B2_Covid" <- Attributes_Top_30_Hashtags$B2_Covid

# Estimate Logit Model of Binary Network

Model_01 <- ergm(Hashtag_Edges_Top_30_Network ~ edges
	+ nodecov("B2_DirectlyReferncesRamNavami")
	+ nodecov("B2_ReligiousConnotation")
	+ nodecov("B2_Covid"), 
	control=control.ergm(12345)
	)
summary(Model_01)

# Estimate Binary ERGM

Model_02 <- ergm(Hashtag_Edges_Top_30_Network ~ 
	nodecov("B2_DirectlyReferncesRamNavami") +
	nodecov("B2_ReligiousConnotation") +
	nodecov("B2_Covid") +
	b1degree(2) +
	edges, 
	control=control.ergm(12345)
	)
summary(Model_02)

# Estimate Relation Event Model (REM)

rawevents <- cbind(eventlist$Hashtag, eventlist$Sender)
evls <- gen.evl(rawevents)
names(evls)
evls$event.key
alpha.ints <- gen.intercepts(evls, basecat="bramnavami2021")

Hashtag_Characteristics <- c("c+d+e+f+v+w+z", "a+h+i+k+l+m+n+o+p+q+r+s+t+u+y+A+B+C+D", "b+g")
Model_03.sforms <- gen.sformlist(evls, Hashtag_Characteristics)
Model_03.ints <- slbind(Model_03.sforms, alpha.ints)
Model_03.ints2 <- sldrop(Model_03.ints, c("bjaishreeram", "bramnavmi", "bjaishriram", "bram",
                                    "bayodhya", "bindia", "blordrama", "bramayana", "bshriram", "bhindu", "bsitaram",
                                    "blordram", "bnavratri", "bstaysafe", "bramanavami2021", "bramanavami", "bramnavmi2021",
                                    "bfestival", "bwednesdaythought", "brama", "bstayhome", "badipurush", "bhinduism", 
                                    "bhappyramnavami2021", "bhanuman", "bhappyramnavami", "bramayan", "bjayshreeram",
                                    "bradhe"))
Model_03 <- rem(evls$eventlist, Model_03.ints2, estimator = "BPM",
                 prior.param=list(mu = 0, sigma = 100, nu = 4))
summary(Model_03)

# Extension of Logit Model of Binary Network

Model_04 <- ergm(Hashtag_Edges_Top_30_Network ~ edges
	+ nodecov("B1_Followed")
	+ nodecov("B1_Followers")
	+ nodecov("B1_Tweets")
	+ nodecov("B2_DirectlyReferncesRamNavami")
	+ nodecov("B2_ReligiousConnotation")
	+ nodecov("B2_Covid"), 
	control=control.ergm(12345)
	)
summary(Model_04)

# Estension of Binary ERGM

Model_05 <- ergm(Hashtag_Edges_Two_or_More_Network ~ 
	nodecov("B2_DirectlyReferncesRamNavami") +
	nodecov("B2_ReligiousConnotation") +
	nodecov("B2_Covid") +
	b1degree(2) +
	edges, 
	control=control.ergm(12345)
	)
summary(Model_05)

# Extension of Relational Event Model (REM)

Model_06 <- rem(evls$eventlist, Model_03.ints, estimator = "BPM",
                 prior.param=list(mu = 0, sigma = 100, nu = 4))
summary(Model_06)
